﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Qianke.Core.Common
{
    public class Email
    {
        /// <summary>
        /// 发件人用户名
        /// </summary>
        public EmailAddress Username { get; set; } = "532151892@qq.com";
        /// <summary>
        /// 发件人邮箱密码
        /// </summary>
        public string Password { get; set; } = "betpclpineqxcbcg";
        /// <summary>
        /// 发送服务器端口号，默认25
        /// </summary>
        public int SmtpPort { get; set; } = 25;
        /// <summary>
        /// 发送服务器地址
        /// </summary>
        public string SmtpServer { get; set; } = "smtp.qq.com";
        /// <summary>
        /// 邮件标题
        /// </summary>
        public string Subject { get; set; }
        /// <summary>
        /// 邮件正文
        /// </summary>
        public string Body { get; set; }
        /// <summary>
        /// 收件人，多个收件人用英文逗号隔开
        /// </summary>
        public string Tos { get; set; }

        /// <summary>
        /// 是否启用SSL，默认已启用
        /// </summary>
        public bool EnableSsl { get; set; } = true;

        /// <summary>
        /// 附件
        /// </summary>
        public List<Attachment> Attachments { get; set; } = new List<Attachment>();




        /// <summary>
        /// 邮件消息对象
        /// </summary>
        private MailMessage GetEmailMessage()
        {
            if (string.IsNullOrEmpty(Tos)) return null;
            var mailMessage = new MailMessage();
            //多个接收者                
            foreach (var str in Tos.Split(','))
            {
                mailMessage.To.Add(str);
            }
            mailMessage.From = new MailAddress(Username, Username);
            mailMessage.Subject = Subject;
            mailMessage.Body = Body;
            mailMessage.IsBodyHtml = false;
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

            mailMessage.Priority = MailPriority.High;
            foreach (var item in Attachments)
            {
                mailMessage.Attachments.Add(item);
            }

            return mailMessage;
        }

        private SmtpClient CreateEmailClient()
        {
            return new SmtpClient
            {
                UseDefaultCredentials = false,
                EnableSsl = EnableSsl,
                Host = SmtpServer,
                Port = SmtpPort,
                Credentials = new System.Net.NetworkCredential(Username, Password),
                DeliveryMethod = SmtpDeliveryMethod.Network,
            };
        }

        public void Send(string desEmil, string title, string body)
        {
            Subject = title;
            Body = body;
            Tos = desEmil;
            Send();
        }

        /// <summary>
        /// 使用同步发送邮件
        /// </summary>
        public void Send()
        {
            using (var message = GetEmailMessage())
            {
                if (message == null) return;
                using(var client = CreateEmailClient())
                    client.Send(message); //异步发送邮件,如果回调方法中参数不为"true"则表示发送失败
            }

        }

    }
}
